$(function () {
   obj.init();
});
var obj = new function () {
    'use strict';
    return {
        dataGrid:null,
        dgState:'close',
        init:function () {
            this.dataGrid = new xhDataGrid($('#list'), 'device/getTabReturn');
            this.dataGrid.parseToolbar().init().activeButton(['bt_list_scanBatchQR'], ['bt_list_scanQR','bt_list_del','bt_list_planContact']);
            this.listener();
            this.funObj().bindDgEvent();
        },
        listener:function () {
            var $this = this;
            //扫码
            $('#bt_list_scanQR').click(function () {
                if(xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().openQR();
            });

            $('#bt_list_scanBatchQR').click(function () {
                if(xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().openBatchQR();
            });

            //搜索
            $('#bt_list_search').click(function () {
                $this.funObj().search();
            });
            xhTool.bindDocKeyDown( $('#bt_list_search'));
            //删除
            $('#bt_list_del').click(function () {
                if(xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().askDelAction();
            });
            //收起
            $('#bt_list_pack').click(function () {
                $this.funObj().pack();
            });
            //展开
            $('#bt_list_expand').click(function () {
                $this.funObj().expand();
            });
            //修改流程关联
            $('#bt_list_planContact').click(function () {
                if(xhExtra.buttonDisabled($(this))) {
                    return false;
                }
               $this.funObj().editPlanContact();
            });

        },
        funObj:function () {
            var $this = this;
            return {
                search:function () {
                    $this.dataGrid.reload({
                        key:xhExtra.getValue($('#list_search')),
                        source:xhExtra.getValue($('#select_source')),
                        states:xhExtra.getValue($('#select_states'))
                    })
                },
                askDelAction:function () {
                    var id = $this.dataGrid.getSelectValue('iRTId');
                    xhExtra.askChangeStatus('device/delTabReturn',{id:id},null,null,function (res) {
                        window.top.xhTop.showSuccess(res.msg);
                        $this.dataGrid.reload();
                    });
                },
                bindDgEvent:function () {
                    $this.dataGrid.command({
                        view:detailview,
                        detailFormatter:function(index,row){
                            var str='<div class="ddv" style="padding:5px 0;overflow: auto;">';
                            if (row['total'].length > 0) {
                                str +=
                                    '<table>' +
                                    '       <tbody>' +
                                    '       <tr><th style="text-align: center">平板型号</th><th style="text-align: center">数目</th></tr>';
                                $.each(row['total'], function (i,item) {
                                    str += '<tr><td style="width: 100px;text-align: center">'+item['name']+'</td><td style="width: 50px;text-align: right">'+item['num']+'</td></tr>';
                                });
                                str += '     </tbody>' +
                                    '</table>';
                                str += '<br>';
                            }

                            if (row['mData'].length > 0) {
                                str +=
                                    '<table>' +
                                    '       <tbody>' ;
                                if (row['iSource'] == 2) {
                                    str+= '       <tr><th style="text-align: center">名称</th><th style="text-align: center">设备类型</th><th style="text-align: center">设备号</th></tr>';
                                    $.each(row['mData'], function (i,item) {
                                        str+='    <tr><td style="width: 220px;"><div >';
                                        str+= item['sSchoolName'];
                                        if(item['sSchoolName']){
                                            str+= '(';
                                            str+= !item['sUserName'] ?  item['sReceiverName'] : item['sUserName'];
                                            str+= ')';
                                        }else  {
                                            str+= !item['sUserName'] ? item['sReceiverName'] : item['sUserName'] ;
                                        }
                                        str+='</div></td></td><td style="width: 100px;">'+item['sDevType']+'</td><td style="width: 170px;">'+item['sDevNumber']+'</td></tr>' ;
                                    });
                                } else if (row['iSource'] == 3){
                                    str+= '       <tr><th style="text-align: center">名称</th><th style="text-align: center">设备类型</th><th style="text-align: center">设备号</th><th style="text-align: center">绑定</th><th style="text-align: center">领用</th></tr>';
                                    $.each(row['mData'], function (i,item) {
                                        str+='    <tr><td style="width: 220px;"><div >';
                                        str+= item['sSchoolName'];
                                        if(item['sSchoolName']){
                                            str+= '(';
                                            str+= !item['sUserName'] ?  item['sReceiverName'] : item['sUserName'];
                                            str+= ')';
                                        }else  {
                                            str+= !item['sUserName'] ? item['sReceiverName'] : item['sUserName'] ;
                                        }
                                            str+='</div></td><td style="width: 100px;">'+item['sDevType']+'</td><td style="width: 170px;">'+item['sDevNumber']+'</td><td style="width: 40px;text-align: center"><div title="'+item['iADLId']+'">'+ (item['bBind'] ? '是':'否') +'</div></td><td style="width: 40px;text-align: center"><div title="'+item['iUTId']+'">'+ (item['bGetTab'] ? '是':'否') +'</div></td></tr>' ;

                                    });
                                } else if (row['iSource'] == 1) { //设备领用
                                    str+= '       <tr><th style="text-align: center">名称</th><th style="text-align: center">领用类型</th><th style="text-align: center">设备类型</th><th style="text-align: center">设备号</th></tr>';
                                    $.each(row['mData'], function (i,item) {
                                        if (item['iReceivePurpose'] == '自用机') {
                                            str+='    <tr><td style="width: 220px;"><div >';
                                            str+= !item['sStaffName'] ? item['sReceiverName'] : item['sStaffName'] ;
                                            str+= '</div></td><td>'+item['iReceivePurpose']+'</td><td style="width: 100px;">'+item['sDevType']+'</td><td style="width: 170px;">'+item['sDevNumber']+'</td></tr>' ;
                                        } else  if (item['iReceivePurpose'] == '学校公用机' || item['iReceivePurpose'] == '教师机' || item['iReceivePurpose'] == '备用机') {
                                            str+='    <tr><td style="width: 220px;"><div >';
                                            str+= item['sSchoolName'];
                                            if(item['sSchoolName']){
                                                str+= '(';
                                                str+= !item['sUserName'] ?  item['sReceiverName'] : item['sUserName'];
                                                str+= ')';
                                            }else  {
                                                str+= !item['sUserName'] ? item['sReceiverName'] : item['sUserName'] ;
                                            }
                                            str+= '</div></td><td>'+item['iReceivePurpose']+'</td><td style="width: 100px;">'+item['sDevType']+'</td><td style="width: 170px;">'+item['sDevNumber']+'</td></tr>' ;
                                        }
                                    });
                                }
                                str += '     </tbody>' +
                                    '</table>';
                            }

                            str+='</div>';
                            return str;
                        },
                        onExpandRow: function(index,row){
                            var ddv = $(this).datagrid('getRowDetail',index).find('div.ddv');
                            ddv.panel({
                                // height:200,
                                width:900,
                                border:false,
                                cache:false,
                                onLoad:function(){
                                    $this.dataGrid.command('fixDetailRowHeight',index);
                                    $this.dataGrid.command('fixDetailRowWidth',index);
                                }
                            });
                        }
                    });
                    $this.dataGrid.onLoadSuccess=function (data) {
                        if($this.dgState == 'open'){
                            $this.funObj().expand();
                        }else if ($this.dgState == 'close'){
                            $this.funObj().pack();
                        }
                    }
                    $this.dataGrid.onDblClickRow = function (index,rowData) {
                        var state = $(rowData['iState']).text();
                        if (state === '待归还') {
                            window.top.xhTop.showModal({
                                title: '修改类型',
                                content: xhTool.getFrameString('device/modifyRetTablet', {iRTId:rowData['iRTId']}, 340),
                                button: '确认',
                                backClose:false
                            })
                        } else if (state == '已归还') {
                            window.top.xhTop.showModal({
                                title: '归还入库明细',
                                content: xhTool.getFrameString('ims/detail/inList', {typeId:rowData['iRTId'],type:2}, 340),
                                backClose:false
                            });
                        }

                    }
                },
                openQR:function () {
                    xhExtra.ajaxRequest('device/getReturnTabQR', {iRTId: $this.dataGrid.getSelectValue('iRTId')}, {check: true}, function(result) {
                        window.top.swal({
                            title: '归还清单-' + $this.dataGrid.getSelectValue('iCreateUserId'),
                            html: '<img src="' + result.msg + '" width="80%">',
                            showConfirmButton: true,
                            confirmButtonText: '确认'
                        }).then(function () {}, function (dismiss) {});
                    });
                },
                openBatchQR:function () {
                    xhExtra.ajaxRequest('device/getRetTabBatchQR', {iRTIdArr: $this.dataGrid.getSelectValues('iRTId')}, {check: true}, function(result) {
                        window.top.swal({
                            title: '批量归还-' + $this.dataGrid.getSelectValue('iCreateUserId'),
                            html: '<img src="' + result.msg + '" width="80%">',
                            showConfirmButton: true,
                            confirmButtonText: '确认'
                        }).then(function () {}, function (dismiss) {});
                    });
                },
                pack:function () {
                    $this.dgState = 'close';
                    var row = $this.dataGrid.command("getRows");
                    for (var r = 0; r < row.length; r++)
                    {
                        $this.dataGrid.command("collapseRow",r);

                    }
                },
                expand:function () {
                    $this.dgState = 'open';
                    var row = $this.dataGrid.command("getRows");
                    for (var r = 0; r < row.length; r++)
                    {
                        $this.dataGrid.command("expandRow",r);
                    }
                },
                editPlanContact:function () {
                    var schoolId = $this.dataGrid.getSelectValue('iSchoolId');
                    var id = $this.dataGrid.getSelectValue('iRTId');
                    var iBPPId = $this.dataGrid.getSelectValue('iBPPId');
                    xhExtra.ajaxRequest('device/ajaxGetPlanName',{schoolId:schoolId},{},function (res) {
                        var html = '';
                        html = '<select id="iBPPId" class="form-control">\n' +
                            '                  <option value=""   style="display: none;">请选择一个流程计划</option>\n';
                        if (res.length > 0) {
                            $.each(res, function (index,item) {
                                html += '      <option value="'+item['id']+'" ';
                                if (iBPPId && iBPPId == item['id']) {
                                    html += ' selected ';
                                }
                                html +='>'+item['name']+'</option>\n' ;
                            });
                        }
                        html +='      </select>';
                        window.top.swal({
                            showCancelButton: true,
                            confirmButtonText: '确认',
                            cancelButtonText: '取消',
                            showLoaderOnConfirm: true,
                            allowOutsideClick: false,
                            title: '修改关联的流程计划',
                            html: html,
                            preConfirm: function() {
                                var iBPPId = $('#iBPPId',window.top.document).val();
                                return new Promise(function (resolve, reject) {
                                    if(!iBPPId){
                                        reject('计划id不能为空！');
                                    }
                                    else {
                                        resolve(iBPPId);
                                    }
                                })
                            }
                        }).then(function (iBPPId) {
                            xhExtra.ajaxRequest('device/modifyRetBpmId',{id:id,iBPPId:iBPPId},{},function (res) {
                                window.top.xhTop.showSuccess(res.msg);
                                $this.dataGrid.reload();
                            })
                        },function (dismiss) {
                            return false;
                        });
                    });

                }
            }
        }
    }
};